package com.app.buss.utils;

import android.content.Context;
import android.graphics.BitmapFactory;

import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.IncapableCause;
import com.zhihu.matisse.internal.entity.Item;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by YuHong on 2019/10/28 0028.
 */
public class GifSizeFilter extends Filter {

    private int mMinWidth;
    private int mMinHeight;
    private int mMaxSize;

    public GifSizeFilter(int minWidth, int minHeight, int maxSizeInBytes) {
        mMinWidth = minWidth;
        mMinHeight = minHeight;
        mMaxSize = maxSizeInBytes;
    }

    @Override
    public Set<MimeType> constraintTypes() {
        return new HashSet<MimeType>() {{
//            add(MimeType.GIF);
            add(MimeType.PNG);
        }};
    }

    @Override
    public IncapableCause filter(Context context, Item item) {
        if (!needFiltering(context, item))
            return null;
        try {
            InputStream inputStream = context.getContentResolver().openInputStream(item.getContentUri());
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(inputStream, null, options);
            int width = options.outWidth;
            int height = options.outHeight;

//                            if (width >= 500)
//                                return new IncapableCause("宽度超过500px");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

//        Point size = PhotoMetadataUtils.getBitmapBound(context.getContentResolver(), item.getContentUri());
//        if (size.x < mMinWidth || size.y < mMinHeight || item.size > mMaxSize) {
//            return new IncapableCause(IncapableCause.DIALOG, context.getString(R.string.error_gif, mMinWidth,
//                    String.valueOf(PhotoMetadataUtils.getSizeInMB(mMaxSize))));
//        }
        return null;
    }

}
